import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'login',
        component: () =>
            import ('../views/login/login_index.vue')
    }, {
        path: '/login', //登录
        name: 'login',
        component: () =>
            import ('../views/login/login_index.vue')
    }, {
        path: '/register', //注册
        name: 'register',
        component: () =>
            import ('../views/login/register_index.vue')
    }, {
        path: '/home', //主页
        name: 'home',
        component: () =>
            import ('../views/home/home_index.vue')
    }, {
        path: '/hotel_register_index', //加入酒店主页
        name: 'hotel_register_index',
        component: () =>
            import ('../views/login/hotel_register/hotel_register_index.vue')
    },
    {
        path: '/hotel_register_join', //登记酒店页
        name: 'hotel_register_join',
        component: () =>
            import ('../views/login/hotel_register/hotel_register_join.vue')
    },
    {
        path: '/home', //首页
        name: 'home',
        component: () =>
            import ('../views/home/home_index.vue'),
        children: [{
                path: '/home/green', //创绿
                name: 'green',
                component: () =>
                    import ('../views/home/green/green_tabbar_index.vue'),

            },
            {
                path: '/home/personcenter', //我的
                name: 'personcenter',
                component: () =>
                    import ('../views/home/personcenter/personcenter_tabbar_index.vue'),
            }
        ]
    },
]


// export default router;

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {
    if (to.path === '/login' || to.path === '/register') {
        next();
    } else {
        let token = localStorage.getItem('userInfo');
        if (token == null || token == '') {
            next('/login');
        } else {
            next();
        }
    }
});

export default router